﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace source_game_caro
{
    public partial class Form1 : Form
    {

        Graphics g;

        int r = 20;
        int c = 20;
        int dd = 20;
        int[,] arr;
        int d = 30;
        int flag;
        //int dem=1;

        public Form1()
        {
            InitializeComponent();
            arr = new int[r + 10, c];
            SetArr(arr, r, c);

        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            g = this.CreateGraphics();
            //ve hang ngang
            for (int i = 0; i <= r; i++)
                g.DrawLine(new Pen(Color.Blue, 1), 200, i * dd, (r * dd) + 200, i * dd);
            //ve cot  
            for (int j = 0; j <= c; j++)
                g.DrawLine(new Pen(Color.Blue, 1), (j * dd) + 200, 0, (j * dd) + 200, c * dd);

        }
        public void SetArr(int[,] arr, int a, int b)
        {
            for (int i = 10; i < a + 10; i++)
            {

                for (int j = 0; j < b; j++)
                {
                    arr[i, j] = 0;
                }
            }
        }
        public void Ve_X(int c, int r)
        {

            Pen p = new Pen(Color.Black, 2);
            g.DrawLine(p, (c * dd), r * dd, (c + 1) * dd, (r + 1) * dd);

            g.DrawLine(p, c * dd, (r + 1) * dd, (c + 1) * dd, r * dd);

        }
        public void Ve_O(int x, int y)
        {
            Pen p1 = new Pen(Color.Red, 2);
            g.DrawEllipse(p1, (x * dd), (y * dd), dd, dd);
        }

        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            // MessageBox.Show((e.X).ToString());
            // MessageBox.Show((e.Y).ToString());
            if (e.X > 200 && e.X < r * 30 && e.Y >= 1 && e.Y < c * 20)
            {
                int x = (int)(((e.X)) / (dd));
                int y = (int)((e.Y) / dd);
                //int t = 0;
                /* if (x == 0)
                 {
                     t = (int)((e.X) / 20);
                 }
                 else if (x == 1)
                 {
                     t = ((int)((e.X) / 20) - 1);
                 }
                 else if (x == 2)
                 {
                     t = ((int)((e.X) / 20) - 2);
                 }*/
                if (e.Button.ToString() == "Left")
                {

                    if (arr[x, y] == 0 && flag == 0)// && dem%2 !=0)
                    {
                        arr[x, y] = 1;
                        Ve_X(x, y);
                        flag = 1;
                        //dem++;
                        if (Total_Check(x, y))
                        {
                            MessageBox.Show("X Thang O Thua");
                            Application.Restart();
                        }

                    }
                    // }

                    //if (e.Button.ToString() == "Left")
                    //{
                    if (arr[x, y] == 0 && flag == 1)// && dem%2 ==0)
                    {
                        arr[x, y] = 2;
                        Ve_O(x, y);
                        flag = 0;
                        //dem++;
                        if (Total_Check(x, y))
                        {
                            MessageBox.Show("Ban O Thang Roi"
                                + "\nBan X Co len hura..");
                            Application.Restart();

                        }


                    }
                }

            }

        }
        //kiem tra ngang 
        public bool kt_Ngang(int a, int b)
        {
            int count = 1;
            int x = a + 1;
            int kt = 0;
            while (x < d && arr[a, b] == arr[x, b])
            {
                count++;
                x++;
            }
            if (arr[x, b] != 0)
            {
                kt = kt + 1;
            }
            x = a - 1;
            while (x >= 0 && arr[a, b] == arr[x, b])
            {
                count++;
                x--;
            }
            if (arr[x, b] != 0)
            {
                kt = kt + 1;
            }
            return (count == 5 && kt != 2) ? true : false;
        }
        //Kiem tra theo hang Doc tinh tu diem vua click chuot
        public bool kt_Doc(int a, int b)
        {
            int count = 1;
            int y = b + 1;
            int kt = 0;
            while (y < r && arr[a, b] == arr[a, y])
            {
                count++;
                y++;
            }
            if (arr[a, y] != 0)
            {
                kt = kt + 1;
            }
            y = b - 1;
            while (y >= 0 && arr[a, b] == arr[a, y])
            {
                count++;
                y--;
            }
            if (arr[a, y] != 0)
            {
                kt = kt + 1;
            }
            return (count == 5 && kt != 2) ? true : false;
        }

        //Kiem tra theo hang Cheo chinh tinh tu diem vua click chuot
        public bool kt_Cheo(int a, int b)
        {
            int count = 1;
            int x = a + 1;
            int y = b + 1;
            int kt = 0;
            while (x < d && y < d && arr[a, b] == arr[x, y])
            {
                count = count + 1;
                x++;
                y++;
            }
            //kiem tra chan hai dau 
            if (arr[x, y] != 0)
            {
                kt = kt + 1;
            }
            // Gan lai gia tri cho bien X, Y;

            x = a - 1;
            y = b - 1;
            while (x >= 0 && y >= 0 && arr[a, b] == arr[x, y])
            {
                count = count + 1;
                x--;
                y--;
            }
            if (arr[x, y] != 0)
            {
                kt = kt + 1;
            }
            return (count == 5 && kt != 0) ? true : false;

        }
        //Kiem tra theo hang Cheo phu tinh tu diem vua click chuot
        public bool kt_CheoPhu(int a, int b)
        {
            int count = 1;
            int x = a + 1;
            int y = b - 1;
            int kt = 0;
            while (x < 30 && y < 20 && arr[a, b] == arr[x, y])
            {
                count++;
                x++;
                y--;
            }
            //kiem tra chan hai dau 
            if (arr[x, y] != 0)
            {
                kt = kt + 1;
            }
            //
            x = a - 1;
            y = b + 1;
            while (x >= 10 && y >= 0 && arr[a, b] == arr[x, y])
            {
                count++;
                x--;
                y++;
            }
            //
            if (arr[x, y] != 0)
            {
                kt = kt + 1;
            }
            return (count == 5 && kt != 2) ? true : false;
        }


        //kiem tra tong hop cac truong hop
        public bool Total_Check(int x, int y)
        {
            //if (Test_Ngang(x, y) == true || Test_Doc(x, y) == true(x, y))
            if (kt_Ngang(x, y) == true || kt_Doc(x, y) == true || kt_Cheo(x, y) == true || kt_CheoPhu(x, y))

                return true;
            return false;
        }




    }
}
